<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>
<script>
    var arr = [10, 20, 30, 50, 22, 86, 12, 8]

    arr.sort(function(obj1, obj2) {
        //改变大于号为小于号，则为倒序
        if (obj1 > obj2) {
            return 1
        } else if (obj1 == obj2) {
            return 0
        } else {
            return -1;
        }
    });
    console.log(arr)

    var arr2 = ['bevel', 'miter', 'children', 'actions', 'zipcode', 'console.log', 'deactivated']

    arr2.sort(function(a, b) {
        //改变大于号为小于号，则为倒序
        if (a > b) {
            return 1
        } else if (a == b) {
            return 0
        } else {
            return -1
        }
    })

    console.log(arr2)

    //Array.sort方法，排序只需要传入函数，和两个比较的参数即可
    //缺点时无法倒序
    console.log(arr.sort(function(num1, num2) {}))
    console.log(arr2.sort(function(str1, str2) {}))
</script>

</html>